<?

/**
* Use $_SESSION["GDate"] if you are using custom date
* or just use the system date 
*
* $today_date = $_SESSION["GDate"];
* $today_date = date("d-m-Y");
*
*/
$today_date		  = date("d-m-Y");

$txType         = _PBL_RETUR;

$srcDbSequence      = "MST_SEQUENCE";
$srcDbCategory      = "MST_MODEL_CAT";
$srcDbGroup         = "MST_MODEL_GRP";
$srcDbHdr			      = "SLS_RET_HDR";
$srcDbHdrLog	      = "LOG_SLS_RET_HDR";
$srcDbDtl           = "SLS_RET_DTL";
$srcDbInputHistory  = "MODEL_INPUT_HISTORY";
$srcMiniInputHdr    = "SLS_RET_MANUAL_SER_NUMBER_HDR";
$srcMiniInputDtl    = "SLS_RET_MANUAL_SER_NUMBER_DTL";

$UID     = unserialize($_SESSION["serUID"]);
$userId  = $UID->getUID();

$CHandlers = new CGHandlers;
$CFetchers = new CGFetchers;

$COracle = new oracleDb;
$COracle->init($_SESSION["ORCL"], 
               $_SESSION["OPASS"],
               $_SESSION["SID"],
               _NO_COMMENT);

if($COracle->connect()){
	
	/** 
	* Delete the previous entry
	*
	*/
	$CHandlers->dbEntDelete($COracle,
            	            $_SESSION["jid"],
            	            $srcDbHdr);
	            
  $CHandlers->dbEntDelete($COracle,
            	            $_SESSION["jid"],
            	            $srcDbDtl);
	
	$CHandlers->dbEntDelete($COracle,
            	            $_SESSION["jid"],
            	            $srcDbInputHistory);
  
  /**
  * We don't build up any jobId anymore since this is
  * a modify mode. Also we already have the previous
  * jobId in $_SESSION
  *
  */
  $jobId = $_SESSION["jid"];
  
  $hdrDate = "TO_DATE('".$_POST["trDateEnt"]."' , 'dd-mm-YYYY')";
  
  /**
  * Insert all data
  *
  */
  for($iRow = 0; $iRow < $dataup; $iRow++){
    
    /**
    * Insert the totals into $srcDbDtl
    *
    */
    $values = " '".$jobId."', ".
              "  ".($iRow + 1)." , ".
              " '".$_POST["modelEnt"][$iRow]."',  ".     /* source Reference (Receive ref) */
              " '".$_POST["qtyEnt"][$iRow]."',    ".     /* quantity         */
              " '".$_POST["serNumEnt"][$iRow]."', ".     /* Total quantity   */
              " '".$_POST["reasonEnt"][$iRow]."'  ";     /* Total Converted  */
              
    insToDbDtl($COracle,
               $values,
               $srcDbDtl);
               
    $valTx   =  " '".$jobId."', ".                        /* Job id     */
                "  ".$hdrDate." , ".                      /* Tr Date    */
                " ' ', ".                                 /* source reference (Receive reference) */
                "  -1 , ".                                /* Category   */
                "  -1 , ".                                /* Group      */
                " '".$_POST["modelEnt"][$iRow]."' , ".    /* model      */
                "  -1 , ".                                /* Model Id   */
                " ' ' , ".                                /* Unit       */
                "  ".$_POST["qtyEnt"][$iRow]." , ".       /* quantity   */
                "  0 , ".                                 /* Receive Qty  */
                " '".$_POST["serNumEnt"][$iRow]."' , ".   /* Serial Number*/
                "  0 , ".                                 /* Model price */
                "  0 ,  ".                                /* horizontal total */
                "  0 , ".                                 /* dicount percent  */
                "  0 , ".                                 /* discount amount  */
                "  0 , ".                                 /* Netto amount     */
                "  ".($iRow + 1).", ".                    /* Source detail number */
                "  ".($iRow + 1).", ".                    /* no, incr-ed by 1 since $iRow starts from 0 */  
                "  ".$txType." , ".                       /* Tx Type        */
                "  '"._NOT_ACTIVE."' ";                   /* This tx status */
    
    insTxHistory($COracle,
                 $valTx,
                 $srcDbInputHistory);
    
    updateManInSrcRef($COracle,
                      $_POST["serNumEnt"][$iRow],
                      $jobId,
                      $srcDbManInHdr);
  }
    
  $CFetchers->fetchHdrNo($COracle,
                         $_POST["trDateEnt"],
                         $srcDbHdr,
                         $hdrNo);
  
  $values = " '".$jobId."', ".                  /* Job id */
            " ' ',    ".                        /* Reference id ' ' default */
            "  ".$hdrDate." , ".                /* Tx date         */
            "  ".$hdrNo.", ".                   /* Number          */
            " '"._NOT_ACTIVE."' ";              /* Status          */
  
  
  insToDbHdr($COracle,
             $values,
             $srcDbHdr);            
            
  /**
  * Insert Log
  *
  */
  $CHandlers->insToDbLog($COracle,
                         $today_date,
                         $jobId,
                         $userId,
                         $_SERVER["REMOTE_ADDR"],
                         _ENTRY_MODE,
                         $srcDbHdrLog);
                               
	$COracle->commit();
	
	if($COracle->getStatus() == true){
	
	  usrMsg("Modify Complete");
	}else{
	  
	  usrMsg("Modify Process Interupted");
	}
	
	$COracle->dc();
	
	/**
	* Pending...
	* I'm not sure if this 
	* reset procedure off
	* causes buggy 
	*
	*	$restore  = 0;
	* $uprun    = 0;
	* $dataup   = 0;
	*
	*/
	//jumpTo(2,"../".$_SESSION["source_caller"]);
	
}else{
	
	echo "<br/><font size='16'>Error reason: upload connection failed </font>\n";
}

?>